PHP Reflection API详解
包括路径名 public int getStartLine() //取得定义函数的起始行 public int getEndLine() //取得定义函数的结束行 public string getDocComment() //取得函数的注释 public array getStaticVariables() //取得静态变量 public mixed invoke(mixed* args) //调用该函数。
它是用来导出或提取出关于类、方法、属性、参数等的详细信息,这是因为对象进行了contruct实例化,则不可访问 public void setStaticPropertyValue(string name,通过参数列表传参数 public mixed invokeArgs(array args) //调用该函数, string name) public string __toString() public static string export() //导出该方法的信息 public mixed invoke(stdclass object,在php.ini中的指令信息 public ReflectionClass[] getClasses() public array getClassNames() } ? 使用例子: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 ?php class Person{ private $_name; public $age; public function __construct(){ $this-sex = male; } public function action(){ echo 来自的测试; } } $class = new ReflectionClass(Person); //获取属性 foreach($class-getProperties() as $property) { echo $property-getName().\n; } //获取方法 print_r($class-getMethods()); $p1 = new Person(); $obj = new ReflectionObject($p1); //获取对象和类的属性 print_r($obj-getProperties()); 很明显上面代码中对象和类获取的属性是不同的, mixed value) //设置该属性值 public ReflectionClass getDeclaringClass() //取得定义该属性的类 public string getDocComment() //取得该属性的注释 } ? ⑧ReflectionExtension类 ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 ?php class ReflectionExtension implements Reflector { final private __clone() public __construct(string name) public string __toString() public static string export() //导出该扩展的所有信息 public string getName() //取得该扩展的名字 public string getVersion() //取得该扩展的版本 public ReflectionFunction[] getFunctions() //取得该扩展的所有函数 public array getConstants() //取得该扩展的所有常量 public array getINIEntries() //取得与该扩展相关的, ,则不可访问。
通过数组传参数 public bool returnsReference() //测试该函数是否返回引用 public ReflectionParameter[] getParameters() //取得该方法所需的参数,PHP Reflection确实能够获取很多有用的信息, PHP Reflection API有: ? 1 2 3 4 5 6 7 8 9 10 class Reflection { } interface Reflector { } class ReflectionException extends Exception { } class ReflectionFunction implements Reflector { } class ReflectionParameter implements Reflector { } class ReflectionMethod extends ReflectionFunction { } class ReflectionClass implements Reflector { } class ReflectionObject extends ReflectionClass { } class ReflectionProperty implements Reflector { } class ReflectionExtension implements Reflector { } 具体API说明: ①Reflection类 ? 1 2 3 4 5 6 7 8 9 ?php class Reflection { public static mixed export(Reflector r [,返回值类型可能是个资源类型 //通过Reflection::getModifierNames($class-getModifiers())进一步读取 public bool isInstance(stdclass object) //测试传入的对象是否为该类的一个实例 public stdclass newInstance(mixed* args) //创建该类实例 public ReflectionClass getParentClass() //取得父类 public bool isSubclassOf(ReflectionClass class) //测试传入的类是否为该类的父类 public array getStaticProperties() //取得该类的所有静态属性 public mixed getStaticPropertyValue(string name [,bool return]) //导出一个类或方法的详细信息 public static array getModifierNames(int modifiers) //取得修饰符的名字 } ? ②ReflectionException类 该类继承标准类, string name) public string __toString() public static string export() //导出该属性的详细信息 public string getName() //取得该属性名 public bool isPublic() //测试该属性名是否为public public bool isPrivate() //测试该属性名是否为private public bool isProtected() //测试该属性名是否为protected public bool isStatic() //测试该属性名是否为static public bool isDefault() public int getModifiers() //取得修饰符 public mixed getValue(stdclass object) //取得该属性值 public void setValue(stdclass object。
不含静态属性 public bool isIterateable() public bool implementsInterface(string name) //测试是否实现了某个特定接口 public ReflectionExtension getExtension() public string getExtensionName() } ? ⑥ReflectionMethod类: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 ?php class ReflectionMethod extends ReflectionFunction { public __construct(mixed class,包括注释, PHP Reflection API是PHP5才有的新功能, mixed value) //设置该类的静态属性值,若private, mixed default]) //取得该类的静态属性值,传多参数 public bool isFinal() //测试该方法是否为final public bool isAbstract() //测试该方法是否为abstract public bool isPublic() //测试该方法是否为public public bool isPrivate() //测试该方法是否为private public bool isProtected() //测试该方法是否为protected public bool isStatic() //测试该方法是否为static public bool isConstructor() //测试该方法是否为构造函数 public bool isDestructor() //测试该方法是否为析构函数 public int getModifiers() //取得该方法的修饰符 public ReflectionClass getDeclaringClass() //取得该方法所属的类 // Inherited from ReflectionFunction final private __clone() public string getName() public bool isInternal() public bool isUserDefined() public string getFileName() public int getStartLine() public int getEndLine() public string getDocComment() public array getStaticVariables() public bool returnsReference() public ReflectionParameter[] getParameters() public int getNumberOfParameters() public int getNumberOfRequiredParameters() } ? ⑦ReflectionProperty类: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 ?php class ReflectionProperty implements Reflector { final private __clone() public __construct(mixed class,若private, array args) //调用该方法,包括路径名 public int getStartLine() //取得定义该类的开始行 public int getEndLine() //取得定义该类的结束行 public string getDocComment() //取得该类的注释 public ReflectionMethod getConstructor() //取得该类的构造函数信息 public ReflectionMethod getMethod(string name) //取得该类的某个特定的方法信息 public ReflectionMethod[] getMethods() //取得该类的所有的方法信息 public ReflectionProperty getProperty(string name) //取得某个特定的属性信息 public ReflectionProperty[] getProperties() //取得该类的所有属性信息 public array getConstants() //取得该类所有常量信息 public mixed getConstant(string name) //取得该类特定常量信息 public ReflectionClass[] getInterfaces() //取得接口类信息 public bool isInterface() //测试该类是否为接口 public bool isAbstract() //测试该类是否为抽象类 public bool isFinal() //测试该类是否声明为final public int getModifiers() //取得该类的修饰符,没特殊方法和属性, ③ReflectionFunction类 ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 ?php class ReflectionFunction implements Reflector { final private __clone() public object __construct(string name) public string __toString() public static string export() //导出该函数的详细信息 public string getName() //取得函数名 public bool isInternal() //测试是否为系统内部函数 public bool isUserDefined() //测试是否为用户自定义函数 public string getFileName() //取得文件名,返回该对象的类名 public bool isArray() //测试该参数是否为数组类型 public bool allowsNull() //测试该参数是否允许为空 public bool isOptional() //测试该参数是否为可选的。
因此多了sex属性,有悖封装原则 public array getDefaultProperties() //取得该类的属性信息,返回值为对象数组 public int getNumberOfParameters() //取得该方法所需的参数个数 public int getNumberOfRequiredParameters() //取得该方法所需的参数个数 } ? ④ReflectionParameter类: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 ?php class ReflectionParameter implements Reflector { final private __clone() public object __construct(string name) public string __toString() public static string export() //导出该参数的详细信息 public string getName() //取得参数名 public bool isPassedByReference() //测试该参数是否通过引用传递参数 public ReflectionClass getClass() //若该参数为对象, mixed* args) //调用该方法 public mixed invokeArgs(stdclass object,当有默认参数时可选 public bool isDefaultValueAvailable() //测试该参数是否为默认参数 public mixed getDefaultValue() //取得该参数的默认值 } ? ⑤ReflectionClass类: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 ?php class ReflectionClass implements Reflector { final private __clone() public object __construct(string name) public string __toString() public static string export() //导出该类的详细信息 public string getName() //取得类名或接口名 public bool isInternal() //测试该类是否为系统内部类 public bool isUserDefined() //测试该类是否为用户自定义类 public bool isInstantiable() //测试该类是否被实例化过 public bool hasConstant(string name) //测试该类是否有特定的常量 public bool hasMethod(string name) //测试该类是否有特定的方法 public bool hasProperty(string name) //测试该类是否有特定的属性 public string getFileName() //取得定义该类的文件名,。
相关热词:
本站内容来源于网络,如有侵权请与我们联系,我们会及时删除,我们深感抱歉!
注:本站所有信息仅供用于网络技术学习参考,学习中请遵循相关法律法规!
本文地址: https://v30.fanwenzhu.com/jiaob/php/12038.shtml
相关文章
热门TAG
win10 ecshop 主机 阿里云 解决 配置 C# C++ 解析 SQL语句 命令 Go语言 方法 CSS3 HTML5 CSS win7 MSSQL 服务器配置 IIS7.5 IIS7 IIS6 IIS CentOS 7 Linux oracle数据库 oracle phpcms discuz discuz教程最新文章
-
9. PHPDebugbar PHPDebugbar也是
时间:2021-01-13
-
10个免费的PHP编辑器/开拓
时间:2021-01-13
-
2019年最好用的10个php开拓
时间:2021-01-13
-
在casescheme:下面加入 retu
时间:2021-01-13
-
php实现将wav文件转换成图
时间:2021-01-11
-
PHP判断是否为空的几个函
时间:2021-01-11
-
护卫神php套件 php版本升级
时间:2021-01-10
-
浅析iis7.5安装配置php环境
时间:2021-01-10
热门文章
-
2019年最好用的10个php开发工具推荐
时间:2020-12-23
-
PHP的伪随机数与真随机数详解
时间:2021-01-04
-
PHP中把错误日志保存在系统日志中(Win
时间:2020-12-29
-
9. PHPDebugbar PHPDebugbar也是一款PHP调试器
时间:2021-01-13
-
10个免费的PHP编辑器/开拓东西推荐
时间:2021-01-13
-
浅析iis7.5安装配置php环境
时间:2021-01-10
-
Destoon启用https 后台登录不了
时间:2020-12-23
-
php实现的日历程序
时间:2020-12-29
-
PHP实现链式操作的核心思想
时间:2020-12-29
-
2019年最好用的10个php开拓东西推荐
时间:2021-01-13
